﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;

namespace DevelopTool.Forms.CommonForms
{
    /// <summary>
    /// 下拉选择窗体
    /// </summary>
    public partial class ComboBoxForm : BaseUIForm
    {
        /// <summary>
        /// 已选择的节点集合
        /// </summary>
        public List<TreeNode> SelectedNodes = new List<TreeNode>();

        /// <summary>
        /// 窗口标题
        /// </summary>
        private string title;

        /// <summary>
        /// 水印文本
        /// </summary>
        private string watermark;

        /// <summary>
        /// 需要选择的节点集合
        /// </summary>
        private List<TreeNode> nodes;

        /// <summary>
        /// 是否至少选择一项
        /// </summary>
        private bool mustBeSelected = false;

        public ComboBoxForm(string title,List<TreeNode> nodes,string watermark = "请选择",bool mustBeSelected = false)
        {
            this.title = title;
            this.nodes = nodes;
            this.watermark = watermark;

            InitializeComponent();
            this.mustBeSelected = mustBeSelected;
        }

        private void ComboBoxForm_Load(object sender,EventArgs e)
        {
            this.Text = title;
            this.comboTreeView.Watermark = watermark;

            this.comboTreeView.Nodes.AddRange(nodes.ToArray());

            ThemHelper.SetSystemThem(this);
        }

        private void btnOk_Click(object sender,EventArgs e)
        {
            foreach(TreeNode item in this.comboTreeView.Nodes)
            {
                if(item.Checked) SelectedNodes.Add(item);
            }

            if(this.mustBeSelected)
            {
                if(SelectedNodes.Count <= 0)
                {
                    this.ShowWarningNotifier("至少需要选择一个项");
                    return;
                }
            }

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender,EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }

}
